//index.js
//获取应用实例
var app = getApp()
Page({
  data: {
    motto: "测试一个小程序",
    userInfo: {},
    heroId:"",
    heroImage:"",
    heroStory:"",
    attru:{},
    attra:{},
    skillu:[],
    skilla:[]
  },
  //下载式神数据
  _loadData: function(url,callback)
  {
    ///https://g37simulator.webapp.163.com/get_hero_story?callback=cb&heroid=200&_=1493708292283
    ///https://g37simulator.webapp.163.com/get_hero_skill?callback=cb&heroid=200&awake=0&level=0&star=2&_=1493708292285
    ///https://g37simulator.webapp.163.com/get_hero_skill?callback=cb&heroid=200&awake=1&level=0&star=2&_=1493708292287
    ///https://g37simulator.webapp.163.com/get_hero_attr?callback=cb&heroid=200&awake=0&level=1&star=2&_=1493708292289
    ///https://g37simulator.webapp.163.com/get_hero_attr?callback=cb&heroid=200&awake=1&level=1&star=2&_=1493708292291

    var self = this;
    wx.request({
      url:url,
      dataType:'text',
      success:function(res)
      {
        try {
          var data = JSON.parse(res.data.substring(3,res.data.length-1));
          if(data.success==true)
          {
            callback.call(self,data.data);
          }
        } catch (error) {
          wx.showToast({"title":"获取试神数据失败"});
          console.log(error);
        }
      },
      fail:function(e)
      {
        wx.showToast({"title":"获取试神数据失败"});
      },
      complete:function(){
        self.loading = false;
      }
    });
  },
  onLoad: function (option) {
    /// 获取式神ID
    this.data.heroId = option.heroId;
    this.setData({heroImage : "http://moysky.qiniudn.com/yys/skin/"+this.data.heroId+".png"});
    var dt = "&heroid="+this.data.heroId+"&_="+(new Date()).getTime();
    this._loadData('https://g37simulator.webapp.163.com/get_hero_story?callback=cb'+dt,function(dt){
      this.setData({heroStory:dt.story[0]});
    });
    this._loadData('https://g37simulator.webapp.163.com/get_hero_skill?callback=cb&awake=0&level=0&star=2'+dt,function(dt){
      for(var skillId in dt)
      {
        if(isNaN(skillId))continue;
        var skill = {desc:"",name:"",image:""};
        skill.desc = dt[skillId].normaldesc;
        for(var i =0;i<dt[skillId].desc.length;i++)
        {
          skill.desc += ("\t\t\n"+dt[skillId].desc[i]);
        }
        skill.name=dt[skillId].name;
        skill.image = "http://moysky.qiniudn.com/yys/skill/"+dt[skillId].icon+".png";
        this.data.skillu.push(skill);
      }
      this.setData({skillu:this.data.skillu});
    });

    this._loadData('https://g37simulator.webapp.163.com/get_hero_skill?callback=cb&awake=1&level=0&star=2'+dt,function(dt){
      //console.log(dt);
      if(!dt)return;
      if(dt.add_type<3)
      {
        for(var skillId in dt)
        {
          if(isNaN(skillId))continue;
          var skill = {desc:"",name:"",image:""};
          skill.desc = dt[skillId].normaldesc;
          for(var i =0;i<dt[skillId].desc.length;i++)
          {
            skill.desc += ("\t\t\n"+dt[skillId].desc[i]);
          }
          skill.name=(dt.add_type==1?"觉醒新增技能：":"觉醒增强技能：")+dt[skillId].name;
          skill.image = "http://moysky.qiniudn.com/yys/skill/"+dt[skillId].icon+".png";
          this.data.skilla.push(skill);
        }
        this.setData({skilla:this.data.skilla});
      }else if(dt.add_type==3)
      {
        this.setData({skilla:[{"desc":("觉醒后"+dt.add)}]});
      }
    });
    this._loadData('https://g37simulator.webapp.163.com/get_hero_attr?callback=cb&awake=0&level=1&star=2'+dt,function(dt){
      dt.attack = Math.round(dt.attack);
      dt.defense = Math.round(dt.defense);
      dt.speed = Math.round(dt.speed);
      dt.maxHp = Math.round(dt.maxHp);
      dt.critPower = (1+dt.critPower)*100;
      this.setData({attru:dt});
    });
    this._loadData('https://g37simulator.webapp.163.com/get_hero_attr?callback=cb&awake=1&level=1&star=2'+dt,function(dt){
      dt = dt || {};
      dt.attack = dt.attack?Math.round(dt.attack):"无觉醒";
      dt.defense = dt.defense?Math.round(dt.defense):"无觉醒";
      dt.speed = dt.speed?Math.round(dt.speed):"无觉醒";
      dt.maxHp = dt.maxHp?Math.round(dt.maxHp):"无觉醒";
      dt.critPower = dt.critPower ? ((1+dt.critPower)*100) : "无觉醒";
      this.setData({attra:dt});
    });
  }
})
